package com.xiangyang.pos.adapter;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xiangyang.pos.R;
import com.xiangyang.pos.bean.HttpBean;
import com.xiangyang.pos.bean.OrderListDataBean;
import com.xiangyang.pos.constant.AppConstant;
import com.xiangyang.pos.http.HttpClient;
import com.xiangyang.pos.http.RequestParameters;
import com.xiangyang.pos.utils.SharedUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/1 0001.
 *
 * 订单列表适配器
 *
 * @author YeDalin
 *
 */

public class OrderListRecyclerAdapter extends RecyclerView.Adapter<OrderListRecyclerAdapter.OrderListViewHolder>{


    private List<OrderListDataBean.OrderListData> list;
    private Context context;

    public OrderListRecyclerAdapter(List<OrderListDataBean.OrderListData> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public OrderListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_list, parent,false);
        OrderListViewHolder viewHolder=new OrderListViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(OrderListViewHolder holder, int position) {

        if(list.size()!=0){
            initView(holder,position);
            initClick(holder,position);
        }

    }


    private void initView(OrderListViewHolder holder, int position){
        holder.tvOrderNumber.setText("订单编号："+list.get(position).getOrdercode());
        holder.tvAllMoney.setText("共计：￥"+list.get(position).getFree());
        holder.tvTime.setText("时间："+list.get(position).getAddtime());
        if(list.get(position).getStatus()==0){
            holder.tvState.setText("支付未完成");
            holder.tvPay.setVisibility(View.VISIBLE);
            holder.tvDelete.setVisibility(View.VISIBLE);
        }else {
            holder.tvState.setText("支付完成");
            holder.tvPay.setVisibility(View.GONE);
            holder.tvDelete.setVisibility(View.GONE);
        }

        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        holder.rvCommodity.setLayoutManager(manager);

        List<OrderListDataBean.OrderData> data=new ArrayList<>();

        for (int i = 0; i < list.get(position).getContent().size(); i++) {
            data.addAll(list.get(position).getContent().get(i).getShopgoods());
        }

        holder.rvCommodity.setAdapter(new OrderDataAdapter(data,context));
    }

    private void initClick(OrderListViewHolder holder, final int position){
        holder.tvPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                choosePay(position);
            }
        });
        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteOrderHttp(position);
            }
        });
    }

    private RequestParameters getDeleteOrderData(int position){
        RequestParameters parameters=new RequestParameters();
        parameters.put("ordercode",list.get(position).getOrdercode());
        parameters.put("shopid",SharedUtils.getStringPrefs(context,"shopId",""));
        return parameters;
    }


    //删除订单
    private void deleteOrderHttp(final int position){


        String UserTOKEN = SharedUtils.getStringPrefs(context, "UserTOKEN", "");
//        final String DELETE_ORDER_URL= AppConstant.POS_URL+"/Order/An/delOrder"+"/u="+UserTOKEN;
        final String DELETE_ORDER_URL= AppConstant.POS_URL+"/Serch/An/delOrder";

        HttpClient httpClient=new HttpClient();
        httpClient.post(DELETE_ORDER_URL, getDeleteOrderData(position), new HttpClient.OnResponseListener() {
            @Override
            public void onResponse(String result) {

                if(!result.equals("Error")){

                    Gson gson=new Gson();
                    HttpBean bean=gson.fromJson(result,HttpBean.class);
                    if(bean!=null){
                        if(bean.getStatus()==1){
                            list.remove(position);
                            notifyDataSetChanged();
                            Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
                        }
                    }

                }else {
                    Toast.makeText(context,"网络异常",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }



    //弹出对话框
    private void choosePay(int position) {

        Dialog dialog = new Dialog(context, R.style.testDialog);


            View inflate = LayoutInflater.from(context).inflate(R.layout.layout_bottom_paymoney, null);
            dialog.setContentView(inflate);

            initPayDialog(inflate);
            initPayDialogClick(dialog,position);



        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);

        dialog.show();

        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //这句就是设置dialog横向满屏了。
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);

    }

    //选择支付对话框控件
    private AppCompatCheckBox checkOne;
    private AppCompatCheckBox checkTwo;
    private AppCompatCheckBox checkThree;
    private AppCompatCheckBox checkFour;
    private TextView tvGoPay;
    private ImageView ivCancel;

    //初始化选择支付方式对话框
    private void initPayDialog(View view){
        checkOne=view.findViewById(R.id.check_one);
        checkThree=view.findViewById(R.id.check_three);
        checkTwo=view.findViewById(R.id.check_two);
        checkFour=view.findViewById(R.id.check_four);
        tvGoPay=view.findViewById(R.id.tv_go_pay);
        ivCancel=view.findViewById(R.id.iv_cancel);
    }

    //初始化选择支付方式对话框点击事件
    private void initPayDialogClick(Dialog dialog,int position){
        checkOne.setOnClickListener(getPayDialogListener(dialog,position));
        checkTwo.setOnClickListener(getPayDialogListener(dialog,position));
        checkThree.setOnClickListener(getPayDialogListener(dialog,position));
        checkFour.setOnClickListener(getPayDialogListener(dialog,position));
        tvGoPay.setOnClickListener(getPayDialogListener(dialog,position));
        ivCancel.setOnClickListener(getPayDialogListener(dialog,position));
    }

    private int payWay=1;

    //选择支付方式对话框的点击事件
    private View.OnClickListener getPayDialogListener(final Dialog dialog, final int position){
        final View.OnClickListener payListener=new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.check_one:
                        payWay=1;
                        checkOne.setChecked(true);
                        checkTwo.setChecked(false);
                        checkThree.setChecked(false);
                        checkFour.setChecked(false);
                        break;
                    case R.id.check_two:
                        payWay=2;
                        checkTwo.setChecked(true);
                        checkOne.setChecked(false);
                        checkThree.setChecked(false);
                        checkFour.setChecked(false);
                        break;
                    case R.id.check_three:
                        payWay=3;
                        checkThree.setChecked(true);
                        checkTwo.setChecked(false);
                        checkOne.setChecked(false);
                        checkFour.setChecked(false);
                        break;
                    case R.id.check_four:
                        payWay=4;
                        checkFour.setChecked(true);
                        checkThree.setChecked(false);
                        checkTwo.setChecked(false);
                        checkOne.setChecked(false);
                        break;
                    case R.id.tv_go_pay:
                        if(payWay==1){

                            if(listener!=null){
                                SharedUtils.setStringPrefs(context,"pay_way","Alipay");
                                getPayData(position);
                                listener.scan();
                            }
                        }else if(payWay==2){
                            if(listener!=null){
                                SharedUtils.setStringPrefs(context,"pay_way","Weixin");
                                getPayData(position);
                                listener.scan();
                            }
                        }else if(payWay==3){
                            Toast.makeText(context,"暂不支持",Toast.LENGTH_SHORT).show();
                        }else if(payWay==4){
                            SharedUtils.setStringPrefs(context,"pay_way","Cash");
                            getPayData(position);
                            if(listener!=null){
                                listener.activityGoTo();
                            }
                        }
                        break;
                    case R.id.iv_cancel:
                        dialog.dismiss();
                        break;
                }
            }
        };
        return payListener;
    }

    private void getPayData(int position){
        SharedUtils.setStringPrefs(context,"orderCode",list.get(position).getOrdercode());
        SharedUtils.setStringPrefs(context,"free",list.get(position).getFree()+"");
    }


    private OrderListListener listener;

    public void setListener(OrderListListener listener) {
        this.listener = listener;
    }

    public interface OrderListListener{
        void scan();
        void activityGoTo();
    }



    @Override
    public int getItemCount() {
        return list.size();
    }

    public class OrderListViewHolder extends RecyclerView.ViewHolder{

        private TextView tvOrderNumber;
        private RecyclerView rvCommodity;
        private TextView tvAllMoney;
        private TextView tvTime;
        private TextView tvState;
        private TextView tvPay;
        private TextView tvDelete;

        public OrderListViewHolder(View itemView) {
            super(itemView);

            tvOrderNumber=itemView.findViewById(R.id.tv_order_number);
            rvCommodity=itemView.findViewById(R.id.rv_commodity);
            tvAllMoney=itemView.findViewById(R.id.tv_all_money);
            tvTime=itemView.findViewById(R.id.tv_time);
            tvState=itemView.findViewById(R.id.tv_state);
            tvPay=itemView.findViewById(R.id.tv_pay);
            tvDelete=itemView.findViewById(R.id.tv_delete);

        }
    }


    public class OrderDataAdapter extends RecyclerView.Adapter<OrderDataAdapter.OrderViewHolder>{

        private List<OrderListDataBean.OrderData> list;
        private Context context;

        public OrderDataAdapter(List<OrderListDataBean.OrderData> list, Context context) {
            this.list = list;
            this.context = context;
        }

        @Override
        public OrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_item_order, parent,false);
            OrderViewHolder viewHolder=new OrderViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(OrderViewHolder holder, int position) {

            if(list.size()!=0){
                holder.tvProductName.setText(list.get(position).getTitle()+"/"
                        +list.get(position).getMeasurement()
                        +list.get(position).getUnit());
                holder.tvPrice.setText("￥"+list.get(position).getPrice());
                holder.tvProductNumber.setText("x"+list.get(position).getNum());
            }

        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public class OrderViewHolder extends RecyclerView.ViewHolder{

            private TextView tvProductName;
            private TextView tvPrice;
            private TextView tvProductNumber;

            public OrderViewHolder(View itemView) {
                super(itemView);

                tvProductName=itemView.findViewById(R.id.tv_product_name);
                tvPrice=itemView.findViewById(R.id.tv_price);
                tvProductNumber=itemView.findViewById(R.id.tv_product_number);

            }
        }
    }

}
